<?php
/**
 * Created by 无所谓已
 * User: Administrator
 * Date: 2025-10-05
 * Time: 20:13
 */

namespace plugin\csauto\app\controller;

use plugin\csauto\app\common\BaseController;
use plugin\csauto\app\model\TorrentReleaseModel;
use plugin\csauto\app\model\SiteModel;
use support\exception\BusinessException;
use support\Request;
use support\Response;

class TorrentReleaseController extends BaseController
{
    /**
     * @var TorrentReleaseModel
     */
    protected $model = null;

    /**
     * 构造函数
     *
     * @return void
     */
    public function __construct()
    {
        $this->model = new TorrentReleaseModel;
    }

    public function index(Request $request)
    {
        return view('torrentrelease/index');
    }

    /**
     * 重写 insert 方法，不支持新增数据
     *
     * @param Request $request
     * @return Response
     */
    public function insert(Request $request): Response
    {
        throw new BusinessException('TorrentRelease 不支持新增数据操作', 403);
    }

    /**
     * 重写 update 方法，支持编辑状态字段
     *
     * @param Request $request
     * @return Response
     */
    public function update(Request $request): Response
    {
        $primary_key = $this->model->getKeyName();
        $id = $request->post($primary_key);

        // 只允许更新状态字段
        $allowedFields = ['task_status', 'status'];
        $data = $request->only($allowedFields);

        // 验证状态值（只允许前端可编辑的状态）
        if (isset($data['task_status']) && !in_array($data['task_status'], [0, 1, 2, 3, 4, 5, 6, 99])) {
            throw new BusinessException('发布状态值无效', 400);
        }
        if (isset($data['status']) && !in_array($data['status'], [1, 2, 99])) {
            throw new BusinessException('任务状态值无效，只能是1（待处理）、2（发布中）或99（完结）', 400);
        }

        if (empty($data)) {
            throw new BusinessException('未指定要更新的字段', 400);
        }

        $model = $this->model->find($id);
        if (!$model) {
            throw new BusinessException('记录不存在', 404);
        }

        foreach ($data as $key => $val) {
            $model->{$key} = $val;
        }
        $model->save();

        return $this->json(0, '更新成功');
    }

    /**
     * 查询后处理数据，添加站点名称映射
     *
     * @param $items
     * @return mixed
     */
    protected function afterQuery($items)
    {
        // 获取所有站点信息用于映射
        $sites = SiteModel::pluck('site_name', 'id')->toArray();

        foreach ($items as $item) {
            // 映射源站名称
            $item->source_site_name = $sites[$item->site_id] ?? '未知站点';

            // 格式化状态显示
            $taskStatusMap = [
                0 => '待发布',
                1 => '发布成功',
                2 => '资源重复',
                3 => '发布失败',
                4 => '任务不存在',
                5 => '任务已结束',
                6 => '源站种子不存在',
                99 => '完结'
            ];
            $item->task_status_text = $taskStatusMap[$item->task_status] ?? '未知状态';

            $statusMap = [
                1 => '待处理',
                2 => '发布中',
                99 => '完结'
            ];
            $item->status_text = $statusMap[$item->status] ?? '未知状态';
        }

        return $items;
    }

}
